package com.example.controller.sys;

import com.example.common.Message;
import com.example.model.ums.RoleMenu;
import com.example.service.ums.RoleMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

@RestController
@RequestMapping("/sys/roleMenu")
@Slf4j
@Valid
public class RoleMenuController {

    @Autowired
    private RoleMenuService service;

    /**
     * 条件查询
     * @param roleMenu
     * @return
     */
    @RequestMapping(value = "/findRoleMenuByParam", method = RequestMethod.GET)
    @ResponseBody
    public Message findRoleMenuByParam(RoleMenu roleMenu) {
        long startTime = System.currentTimeMillis();
        log.info("RoleMenuController.findRoleMenuByParam 入参 roleMenu ->{},start by startTime ->{}", roleMenu, startTime);
        List<RoleMenu> roleMenus = service.findRoleMenuByParam(roleMenu);
        log.info("RoleMenuController.findRoleMenuByParam 耗时：{} ms", System.currentTimeMillis() - startTime);
        return Message.ok(roleMenus);
    }

    /**
     * 新增
     * @param roleMenu
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Message insert(@Valid RoleMenu roleMenu) throws Exception {
        long startTime = System.currentTimeMillis();
        log.info("RoleMenuController.insert 入参 roleMenu ->{},start by startTime ->{}", roleMenu, startTime);
        if (null == roleMenu) {
            throw new Exception("参数异常.");
        }
        Integer rlt = service.insert(roleMenu);
        log.info("RoleMenuController.insert 耗时：{} ms", System.currentTimeMillis() - startTime);
        return Message.ok(rlt);
    }

    /**
     * 分配权限
     * @param roleId
     * @param menuIds
     * @return
     */
    @RequestMapping(value = "/distribution", method = RequestMethod.GET)
    @ResponseBody
    public Message distribution(@RequestParam @NotBlank(message = "roleId不能为空") String roleId, @RequestParam List<String> menuIds) {
        long startTime = System.currentTimeMillis();
        log.info("RoleMenuController.distribution 入参 roleId ->{},menuIds ->{},start by startTime ->{}", roleId, menuIds, startTime);
        service.distribution(roleId, menuIds);
        log.info("RoleMenuController.distribution 耗时：{} ms", System.currentTimeMillis() - startTime);
        return Message.ok();
    }


}
